<template>
  <el-dialog title="新增盘点计划"
             :visible.sync="dialogForm"
             width="1200px">
    <el-form :inline="true"
             :model="condition"
             class="search-form"
             ref="aList"
             label-width="100px">
      <el-row class="row">
        <!-- <el-col :span="12">
          <el-form-item label="盘点计划编号"
                        prop="planId">
            <el-input v-model="form.planId"
                      placeholder=""></el-input>
          </el-form-item>
        </el-col> -->
        <el-col :span="8">
          <el-form-item label="库房"
                        prop="warehouseName">
            <el-input v-model="form.warehouseName"
                      placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="库房"
                        prop="roomName">
            <el-input v-model="form.roomName"
                      placeholder=""></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="备注"
                        prop="remark">
            <el-input v-model="form.remark"
                      placeholder=""></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="CountData"
              border
              max-height="500">
      <el-table-column type="expand"
                       width="60">
        <template slot-scope="props">
          <el-form label-position="left"
                   inline
                   class="demo-table-expand">
            <el-col :span="6"
                    class="table-expand-style">
              <el-form-item label="验收数量">
                <span>{{ props.row.warehouseName}}</span>
              </el-form-item>
            </el-col>
            <el-col class="table-expand-style">
              <el-form-item label="计量方式">
                <span>{{ props.row.measureWay | _filterMeterStyle}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6"
                    class="table-expand-style">
              <el-form-item label="管理方式">
                <span>{{ props.row.manageWay | _filterManagement}}</span>
              </el-form-item>
            </el-col>
            <el-col class="table-expand-style">
              <el-form-item label="吊牌重量">
                <span>{{ props.row.createBy}}</span>
              </el-form-item>
            </el-col>
            <el-col class="table-expand-style">
              <el-form-item label="车船号">
                <span>{{ props.row.createDateTime}}</span>
              </el-form-item>
            </el-col>
            <el-col class="table-expand-style">
              <el-form-item label="物资ID">
                <span>{{ props.row.remark}}</span>
              </el-form-item>
            </el-col>
          </el-form>
        </template>
      </el-table-column>
      <el-table-column label="序号"
                       type="index"
                       width="50"></el-table-column>
      <el-table-column prop="cargoOwnerName"
                       label="货主"
                       min-width="120"></el-table-column>
      <el-table-column prop="material"
                       label="品名"
                       min-width="80"></el-table-column>
      <el-table-column prop="madeBy"
                       label="规格"
                       min-width="80"></el-table-column>
      <el-table-column prop="balanceWeight"
                       label="材质"
                       min-width="80"></el-table-column>
      <el-table-column prop="balanceNum"
                       label="生产厂家"
                       min-width="80"></el-table-column>
      <el-table-column prop="preWeight"
                       label="仓库"
                       min-width="80"></el-table-column>
      <el-table-column prop="preWeight"
                       label="库房"
                       min-width="80"></el-table-column>
      <el-table-column prop="preWeight"
                       label="库位"
                       min-width="80"></el-table-column>
      <el-table-column prop="preWeight"
                       label="验收件数"
                       min-width="80"></el-table-column>
      <el-table-column prop="preWeight"
                       label="验收质量"
                       min-width="80"></el-table-column>
    </el-table>

    <span slot="footer"
          class="dialog-footer">
      <el-button @click="dialogForm = false">取 消</el-button>
      <el-button class="ml-80"
                 type="primary"
                 @click="handleSubmit(false)"
                 :loading="$store.getters.loadingBtn">保 存</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addCount,
} from '@/api/matter/cheackPlan'
import autoSearchName from '@/components/autocomplete/autoSearchName' //货主组件
import autoCargo from '@/components/autocomplete/autoCargo'
import { getWarehouseList } from '@/api/adminWarehouse/adminWarehouse' //所属仓库来源于仓库设置

export default {
  name: 'addCountForm',
  components: {
    autoCargo,
    autoSearchName
  },
  data() {
    return {
      form: this.clearForm(),
      CountData: [],
      dialogForm: false,
      component: '',
      condition: {}, // 查询条件
      id: '',
    }
  },
  methods: {
    clearForm() {
      return {
        orgId: '',
        orgId:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.id,
        orgName:
          this.$store.state.user.currentUser.org &&
          this.$store.state.user.currentUser.org.name,
        ids: [],
        remark: "",
        roomCode: "",
        roomName: "",
        warehouseCode: "",
        warehouseName: "",
        planId: ''
      }
    },
    handleSubmit() {
      let that = this;
      for(var i=0;i<that.CountData.length;i++) {
        that.form.ids.push(that.CountData[i].inventoryId)
      }
      console.log(that.form)
      addCount(that.form).then(function(res) {
        that.$message({ message: '新增成功!', type: 'success' })
        that.dialogForm = false
        that.$emit('load')
      })
    },
    getSummaries(param) {
      const { columns, data } = param
      const sums = []
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计'
          return
        }
        const values = data.map(item => Number(item[column.property]))
        if (column.property === 'balanceWeight') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] += ' 吨'
        } else if (column.property === 'balanceNum') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] += ' 件'
        } else if (column.property === 'preWeight') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] += ' 吨'
        } else if (column.property === 'balanceCount') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr)
            if (!isNaN(value)) {
              return prev + curr
            } else {
              return prev
            }
          }, 0)
          sums[index] += ' 个'
        } else {
          sums[index] = ''
        }
      })
      return sums
    },
    handleRouterPush(val) {
      this.$store.commit('ADD_CACHED_VIEWS', val)
      this.$router.push(val.path)
    }
  }
}
</script>

<style>
.control-line {
  height: 30px;
}
.line {
  border-left: solid 5px #00a1ff;
  height: 18px;
  margin-top: 5px;
  display: inline-block;
  vertical-align: sub;
}
.gg-input {
  margin-bottom: 0 !important;
}
.gg-input .el-form-item__content .el-input {
  width: 100px !important;
}
.search-width8 {
  width: 450px;
}
.base-list .search .border-line {
  margin-top: 0px !important;
}
.table-expand-style {
  width: 300px;
}
.table-expand-style label {
  color: #99a9bf;
}
</style>
